package com.github.gaojh.mvc.setup;

import com.github.gaojh.ioc.IocUtil;
import com.github.gaojh.ioc.annotation.Setup;
import com.github.gaojh.ioc.bean.ClassInfo;
import com.github.gaojh.ioc.bean.ClassScanner;
import com.github.gaojh.ioc.bean.Initializer;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author gaojh
 * @since 2020/3/25
 */
@Slf4j
public class SetupInitializer implements Initializer {
    @Override
    public void init(ClassScanner classScanner) throws Exception {
        List<ClassInfo> list = classScanner.getClassByAnnotation(Setup.class);
        for (ClassInfo classInfo : list) {
            Setup setup = classInfo.getBeanClass().getAnnotation(Setup.class);
            if (!SetupAdapter.class.isAssignableFrom(classInfo.getBeanClass())) {
                log.error("注册启动器失败：{} 需要实现SetupAdapter接口，本次初始化忽略！", classInfo.getBeanName());
                return;
            }

            SetupAdapter setupAdapter = (SetupAdapter) IocUtil.getBean(classInfo.getBeanClass());
            SetupContext.addSetupRunner(setup.order(), setupAdapter);
            log.debug("注册启动器：{}", classInfo.getBeanName());
        }
    }
}
